/**
 * hookEmitt - EventBus 事件总线
 */
import mitt from "mitt";
import { Static, onBeforeUnmount } from "vue";

interface MittOption {
    /** 事件名称 */
    name: string;
    /** 回调 */
    callback: Fn;
}

/**
 * 必须在 hookEmitt 函数体外面
 */
const emitter = mitt();

/**
 * 事件总线钩子
 * @param option
 * @returns
 */
export const hookEmitt = (option?: MittOption) => {
    if (option) {
        emitter.on(option.name, option.callback);

        /** 挂载之前先关闭 */
        onBeforeUnmount(() => {
            emitter.off(option.name);
        });
    }

    return { emitter };
};
